package com.kehao.leetcode.basic.math;

import org.junit.Test;

public class RomeSolution {
    public int romanToInt(String s) {
        char[] charArray = s.toCharArray();
        int result = 0;
        for (int i = 0; i < charArray.length;) {
            if(charArray[i]=='I'){
                if(i!=charArray.length-1 && charArray[i+1]=='V'){
                    result+=4;
                    i+=2;
                }else if(i!=charArray.length-1 && charArray[i+1]=='X'){
                    result+=9;
                    i+=2;
                }else {
                    result+=1;
                    i++;
                }
            }else if(charArray[i]=='V'){
                result+=5;
                i++;
            }else if(charArray[i]=='X'){
                if(i!=charArray.length-1 && charArray[i+1]=='L'){
                    result+=40;
                    i+=2;
                }else if(i!=charArray.length-1 && charArray[i+1]=='C'){
                    result+=90;
                    i+=2;
                }else {
                    result+=10;
                    i++;
                }
            }else if(charArray[i]=='L'){
                result+=50;
                i++;
            }else if(charArray[i]=='C'){
                if(i!=charArray.length-1 && charArray[i+1]=='D'){
                    result+=400;
                    i+=2;
                }else if(i!=charArray.length-1 && charArray[i+1]=='M'){
                    result+=900;
                    i+=2;
                }else {
                    result+=100;
                    i++;
                }
            }else if(charArray[i]=='D'){
                result+=500;
                i++;
            }else if(charArray[i]=='M'){
                result+=1000;
                i++;
            }
        }
        return result;
    }

    @Test
    public void test01(){
        String s =  "MCMXCIV";
        int roman = romanToInt(s);
        System.out.println(roman);
    }
}
